# -*- mode: Makefile -*-
#
# Copyright (C) 2013-2016 DNAnexus, Inc.
#
# This file is part of dx-toolkit (DNAnexus platform client libraries).
#
#   Licensed under the Apache License, Version 2.0 (the "License"); you may not
#   use this file except in compliance with the License. You may obtain a copy
#   of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#   WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#   License for the specific language governing permissions and limitations
#   under the License.

# curl_build_dir := $(shell mktemp -d --tmpdir=/tmp curl-build-XXXXXX)

# curl:
# 	$(DNANEXUS_HOME)/src/dx-verify-file/build_curl.sh $(curl_build_dir)
UNAME := $(shell uname)

ifneq ($(UNAME), MINGW32_NT-6.2)
	$(error This Makefile should be used only with MINGW32_NT-6.2)
endif

DXTOOLKIT_GITVERSION := $(shell git describe|sed 's/-/+/')
VERSION = 0.0.1

sw_dir = $(HOME)/sw/local
curl_dir = $(sw_dir)/curl
cpp_dir = $(DNANEXUS_HOME)/src/cpp
dxjson_dir = $(cpp_dir)/dxjson
dxhttp_dir = $(cpp_dir)/SimpleHttpLib
dxcpp_dir = $(cpp_dir)/dxcpp
dx-verify-file_dir = $(DNANEXUS_HOME)/src/dx-verify-file
zlib_dir = $(sw_dir)/zlib-1.2.3-lib
lmagic_dir = $(sw_dir)/file-5.03-lib
boost_dir = $(sw_dir)/boost_1_51_0
VPATH = $(dxjson_dir):$(dxhttp_dir):$(dxcpp_dir):$(dx-verify-file_dir)

CFLAGS = -g -Wall -Wextra -I/include -I$(zlib_dir)/include
CXXFLAGS = -DSTATIC_BUILD -DWINDOWS_BUILD -DBOOST_THREAD_USE_LIB -D_FILE_OFFSET_BITS=64 -DDX_VERIFY_FILE_VERSION=\"$(VERSION)\" -DDXTOOLKIT_GITVERSION=\"$(DXTOOLKIT_GITVERSION)\" -g -Wall -Wextra -Werror=return-type -std=gnu++0x -I$(curl_dir)/include -I$(cpp_dir) -I$(dxhttp_dir) -I$(dxjson_dir) -I$(dxcpp_dir) -I$(dx-verify-file_dir) -I$(boost_dir) -I$(zlib_dir)/include -I/include

LDFLAGS := -static-libstdc++ -static-libgcc -DBOOST_THREAD_USE_LIB -L$(boost_dir)/stage/lib -L$(curl_dir)/lib -L/lib $(LDFLAGS) -L$(zlib_dir)/lib -lboost_program_options-mgw47-mt-1_51 -lboost_filesystem-mgw47-mt-1_51 -lboost_regex-mgw47-mt-1_51 -lboost_system-mgw47-mt-1_51 -lcurl -lcrypto -lz -lboost_thread-mgw47-mt-1_51 -lboost_chrono-mgw47-mt-1_51

dxjson_objs = dxjson.o
dxhttp_objs = SimpleHttp.o SimpleHttpHeaders.o Utility.o
dxcpp_objs = api.o dxcpp.o SSLThreads.o utils.o dxlog.o 
dx-verify-file_objs = options.o log.o chunk.o main.o File.o

dxjson: $(dxjson_objs)
dxhttp: $(dxhttp_objs)
dxcpp: $(dxcpp_objs)
dx-verify-file: $(dx-verify-file_objs)

all: dxjson dxhttp dxcpp dx-verify-file
	g++ *.o $(LDFLAGS) -o dx-verify-file

dist: all
	rm -rf dx-verify-file-$(VERSION)-windows dx-verify-file-$(VERSION)-windows.zip
	mkdir -p dx-verify-file-$(VERSION)-windows
	mv dx-verify-file.exe dx-verify-file-$(VERSION)-windows/
	../windows_build/collect_dx-verify-file_dlls.sh dx-verify-file-$(VERSION)-windows
	cd dx-verify-file-$(VERSION)-windows && zip -9 -r ../dx-verify-file-$(VERSION)-windows.zip .
	rm -rf dx-verify-file-$(VERSION)-windows
clean:
	rm -v *.o dx-verify-file

.PHONY: all dxjson dxhttp dxcpp dx-verify-file
